package sng.modules.view
{
	
	import com.greensock.plugins.FrameBackwardPlugin;
	
	import components.IconMoney;
	import components.IconPower;
	import components.MainMenu;
	import components.TopIconGroup;
	
	import core.event.BuildingEvent;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.setTimeout;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.Label;
	import mx.events.ItemClickEvent;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import sng.data.info.GlobalInfo;
	import sng.modules.model.vo.BuildingVO;
	import sng.modules.model.vo.HudVO;
	import sng.modules.model.vo.MainVO;
	import sng.modules.system.CitySystem;
	import sng.modules.system.NameSpace;
	import sng.modules.system.SocialSystem;
	import sng.modules.view.components.hud.HUDmain;
	
	import spark.components.Button;
	import spark.components.List;
	import spark.components.TabBar;
	import spark.components.ToggleButton;
	import spark.events.IndexChangeEvent;
	
	/**
	 * 
	 * @author leonliu
	 * 主界面中介
	 * 
	 */	
	public class HudMediator extends Mediator
	{
		
		/**
		 *主界面游戏属性 
		 */		
		//private  var m_hudvo:MainVO;
		private var main:Main;
		
		//当前用户数据
		private var hudvo:HudVO;
		//好友用户数据
		private var friendvo:HudVO;
		
		private var m_hudmain:HUDmain;
		private var m_currenttype:int=0;//当前建筑的类型
		
		public function HudMediator(app:Object)
		{
			super(NameSpace.HudMediatorName,app);
			
			main=app as Main;
			hudvo=new HudVO();
			m_hudmain=new HUDmain();
			main.uicf.addChild(m_hudmain);//加载主界面
			
			var tabbar:TabBar=m_hudmain.mainMenu.tabbarBuilding;
			tabbar.addEventListener(MouseEvent.CLICK,onBuildingTypeChange);
			m_hudmain.mainMenu.listBuilding.addEventListener(MouseEvent.CLICK,onBuild);// IndexChangeEvent.CHANGE.addEventListener(MouseEvent.CLICK, onBuild);
			
			m_hudmain.addEventListener(NameSpace.HudInitData,onHudInitData);//此事件由UI的hud1_creationCompleteHandler事件抛出
			m_hudmain.addEventListener(CitySystem.CityZoomIn,zoominHandler);//此事件由UI的hud1_creationCompleteHandler事件抛出
			m_hudmain.addEventListener(CitySystem.CityZoomOut,zoomoutHandler);//此事件由UI的hud1_creationCompleteHandler事件抛出
			m_hudmain.addEventListener(CitySystem.CityFull,fullCity);
			
			
			var tBtn:Button = m_hudmain.btngroupoperation1.tBtnDelete;
			tBtn.addEventListener(MouseEvent.CLICK, onDeleteBuild);
			
			tBtn= m_hudmain.btngroupoperation1.tBtnTurn;
			tBtn.addEventListener(MouseEvent.CLICK, onTurnBuild);
			
			tBtn = m_hudmain.btngroupoperation1.tBtnMove;
			tBtn.addEventListener(MouseEvent.CLICK, onMoveBuild);
			
			//tBtn = m_hudmain.mainMenu.tb;
			m_hudmain.tBtnCancel.addEventListener(MouseEvent.CLICK, onCancel);
			m_hudmain.btngroupoperation1.tBtnCancel.addEventListener(MouseEvent.CLICK, onCancel);
			m_hudmain.btnHome.addEventListener(MouseEvent.CLICK,onHome);
			
			addAction();
			
			setTimeout(setGlobalPositionInfo, 500);
		}
		
		private function setGlobalPositionInfo():void
		{
			var point:Point = new Point(100, 0);
			point = m_hudmain.TopIcon.IconMoney.localToGlobal(point);
			GlobalInfo.coinBarX = point.x;
			GlobalInfo.coinBarY = point.y;
			
			point = new Point(100, 0);
			point = m_hudmain.TopIcon.IconSatisfaction.localToGlobal(point);
			GlobalInfo.goodsBarX = point.x;
			GlobalInfo.goodsBarY = point.y;
			
			point = new Point();
			point = m_hudmain.TopIcon.IconExp.localToGlobal(point);
			GlobalInfo.expBarX = point.x;
			GlobalInfo.expBarY = point.y;			
		}
		
		/**
		 * 
		 * @return 
		 * 功能：监听通知
		 * 
		 */		
		override public function listNotificationInterests():Array
		{
			return [ 
				NameSpace.HudInitComplete,
				NameSpace.HudbuildListChange,
				SocialSystem.SocialFriendsCityEntry,
				SocialSystem.SocialFriendsDataComplete
			];
		}
		
		/**
		 * 
		 * @param notification
		 * 功能：响应通知
		 * 
		 */		
		override public function handleNotification(notification:INotification):void
		{
			m_hudmain.btnHome.enabled=false;
			switch (notification.getName()){
				
				case NameSpace.HudInitComplete:
				{
					
					BindingUtils.bindProperty(m_hudmain,"width",main.uicf,"width");
					BindingUtils.bindProperty(m_hudmain,"height",main.uicf,"height");
				
					
					hudvo==notification.getBody() as HudVO;
					
					BindingUtils.bindProperty(m_hudmain.heroinfo.rTextUserName,"text",hudvo.mainvo,"usernick");
					BindingUtils.bindProperty(m_hudmain.heroinfo.rTextLevel,"text",hudvo.mainvo,"level");
					BindingUtils.bindProperty(m_hudmain.TopIcon.IconMoney.rTextMoney,"text",hudvo.mainvo,"money");
					
					BindingUtils.bindProperty(m_hudmain.TopIcon.IconSatisfaction.rTextSatisfaction,"text",hudvo.mainvo,"goods");
					//绑定新的UI eason 20110718
					BindingUtils.bindProperty(m_hudmain.TopIcon.IconPower.energyUI,"currentValue",hudvo.mainvo,"energy");
					//双向绑定
					BindingUtils.bindProperty(hudvo.mainvo,"energy",m_hudmain.TopIcon.IconPower.energyUI,"currentValue");
					
					BindingUtils.bindProperty(m_hudmain.TopIcon.IconExp.expUI,"currentValue",hudvo.mainvo,"experience");
					
					BindingUtils.bindProperty(m_hudmain.TopIcon.IconPopulation.rTextPopulation,"text",hudvo.mainvo,"personInfo");

					
					m_hudmain.enabled=true;
					createCity(hudvo,false);
					m_currenttype=0;//当前的建筑类型
					BindBuildList();//绑定建筑列表
					break;
				}
				case SocialSystem.SocialFriendsCityEntry://进入朋友城市请求
				{
					friendvo=notification.getBody() as HudVO;
					//增加界面控制逻辑
					m_hudmain.mainMenu.enabled=false;
					m_hudmain.btnHome.enabled=true;
					createCity(friendvo,true);
					break;
				}
				case SocialSystem.SocialFriendsDataComplete:
				{
					var fList:ArrayCollection = notification.getBody() as ArrayCollection;
					m_hudmain.menuFriend.listFriends.dataProvider = fList;
					break;
				}
				default:
					break;
			}
		}
		
		private function addAction():void
		{
			m_hudmain.menuFriend.listFriends.addEventListener(IndexChangeEvent.CHANGE, enterFriendCity);
		}
		
		protected function enterFriendCity(event:IndexChangeEvent):void
		{
			var list:List = event.currentTarget as List;
			var data:Object = (event.currentTarget as List).selectedItem;
			sendNotification(SocialSystem.SocialFriendsCityEntryRequest, data.f_id);
			
		}
		
		/**
		 *创建用户城市 
		 * 
		 */		
		private function createCity(_hudvo:HudVO,blFreid:Boolean):void
		{
			if (blFreid)
				sendNotification(NameSpace.HudCreateFriendCity,_hudvo);
			else
				sendNotification(NameSpace.HudCreateUserCity,_hudvo);//发送创建用户城市消息
		}
		
		/**
		 *绑定建筑列表 
		 * 
		 */		
		private function BindBuildList():void
		{
			hudvo.buildinglist.filterFunction=filterFunc;
			hudvo.buildinglist.refresh();
			m_hudmain.mainMenu.listBuilding.dataProvider=hudvo.buildinglist;
			
		}
		
		private function filterFunc(value:Object):Boolean
		{
			return (value.tabid==m_currenttype);
		}
		
		/**
		 *初始化界面数据 
		 * 
		 */		
		private function onHudInitData(evt:Event):void
		{
			sendNotification(NameSpace.HudInitData, hudvo);
		}
		
		//城市放大处理
		protected function zoominHandler(event:Event):void
		{
			zoomHandler(1);
		}
		
		//城市缩小处理
		protected function zoomoutHandler(event:Event):void
		{
			zoomHandler(-1);
		}
		
		private function zoomHandler(step:int):void
		{
			sendNotification(CitySystem.CityZoomNotice, step);
		}		
		
		private function onBuildingTypeChange(evt:MouseEvent):void
		{
			m_currenttype=m_hudmain.mainMenu.tabbarBuilding.selectedIndex;
			BindBuildList();
		}
		
		private function onBuild(evt:MouseEvent):void
		{
			var _buinding:BuildingVO=m_hudmain.mainMenu.listBuilding.selectedItem as BuildingVO;
			if (buildJudge(_buinding))
			{
				onCancel(null);
				if (_buinding.buildingid>=1000)//播种状态
				{
					sendNotification(NameSpace.HudBuildSeed,_buinding.buildingid);
				}
				else//建设状态
				{
					sendNotification(NameSpace.HudBuildRequest, _buinding);
				}
			}
		}
		
		/**
		 * 
		 * @param event
		 * 删除建筑
		 * 
		 */		
		private function onDeleteBuild(event:Event):void
		{
//			if (!m_hudmain.btngroupoperation1.tBtnDelete.selected)
//			{
//				sendNotification(NameSpace.HudBuildCancel);
//			}
//			else
//			{
				sendNotification(NameSpace.HudBuildDelete);
//				m_hudmain.btngroupoperation1.tBtnMove.selected=false;
//				m_hudmain.btngroupoperation1.tBtnTurn.selected=false;
//				
//			}
		}
		
		/**
		 * 
		 * @param event
		 * 建筑物转向
		 * 
		 */		
		private function onTurnBuild(event:Event):void
		{
			
			
//			if (!m_hudmain.btngroupoperation1.tBtnTurn.selected)
//			{
//				sendNotification(NameSpace.HudBuildCancel);
//			}
//			else
//			{
				sendNotification(NameSpace.HudBuildTurn);
//				m_hudmain.btngroupoperation1.tBtnMove.selected=false;
//				m_hudmain.btngroupoperation1.tBtnDelete.selected=false;
//				
//			}
		}
		
		/**
		 * 
		 * @param event
		 * 移动建筑物
		 * 
		 */		
		private function onMoveBuild(event:Event):void
		{
//			if (!m_hudmain.btngroupoperation1.tBtnMove.selected)
//			{
//				sendNotification(NameSpace.HudBuildCancel);
//			}
//			else
//			{
				sendNotification(NameSpace.HudBuildMove);
//				m_hudmain.btngroupoperation1.tBtnTurn.selected=false;
//				m_hudmain.btngroupoperation1.tBtnDelete.selected=false;
//				
//			}
		}
		
		/**
		 * 
		 * @param event
		 * 取消当前状态
		 * 
		 */		
		private function onCancel(event:Event):void
		{
			sendNotification(NameSpace.HudBuildCancel);
		}
		
		private function onHome(event:Event):void
		{
			//返回到自己城市
			m_hudmain.btngroupoperation1.enabled=true;
			m_hudmain.btnHome.enabled=false;
			m_hudmain.mainMenu.enabled=true;
			createCity(hudvo,false);
		}
		
		
		private function buildJudge(bvo:BuildingVO):Boolean
		{
			m_hudmain.currentState = "State1";
			if (bvo.buildingprice>hudvo.mainvo.money)
			{
				Alert.show("亲，金币不够用！充点Q币吧");
				return false;
			}
			else if (bvo.populationbuildlimit>(hudvo.mainvo.totalpersonnum-hudvo.mainvo.personnum))
			{
				Alert.show("亲，人口总量不够，请增加市政建筑！");
				return false;
			}
			else
			{
				
				return true;
			}
		}
		
		private function fullCity(event:Event):void//全屏
		{
//			trace(m_hudmain.width,m_hudmain.height);
			m_hudmain.width=main.uicf.width;
			m_hudmain.height=main.uicf.height;
			//传入高宽
			sendNotification(CitySystem.CityFullRequest,[m_hudmain.width,m_hudmain.height]);

			setTimeout(setGlobalPositionInfo, 500);
		}
	}
}